package com.xiaochi.auth.domain.service.impl;

import com.xiaochi.auth.common.enums.IsDeletedEnum;
import com.xiaochi.auth.domain.convert.AuthRolePermissionBOConverter;
import com.xiaochi.auth.domain.entity.AuthRolePermissionBO;
import com.xiaochi.auth.domain.service.AuthRolePermissionDomainService;
import com.xiaochi.auth.infra.basic.entity.AuthRolePermission;
import com.xiaochi.auth.infra.basic.service.AuthRolePermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zqx
 * @since 2024/8/29
 */
@Service
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePermissionService authRolePermissionService;

    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {

       /* AuthRolePermission authRolePermission = AuthRolePermissionBOConverter.INSTANCE.authRolePermissionBOToEntity(authRolePermissionBO);
        authRolePermission.setIsDeleted(IsDeletedEnum.UN_DELETED_ENUM.getCode());
        Integer count = authRolePermissionService.insert(authRolePermission);
        return count > 0;*/
        /*这是一种方法，只能用于数据量小的*/
        /*List<Long> permissionIdList = authRolePermissionBO.getPermissionIdList();
        Long roleId = authRolePermissionBO.getRoleId();
        permissionIdList.forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setIsDeleted(IsDeletedEnum.UN_DELETED_ENUM.getCode());
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermissionService.insert(authRolePermission);
        });
        return true;*/
        /*这个批量插入*/
        List<AuthRolePermission> authRolePermissionList  = new LinkedList<>();
        List<Long> permissionIdList = authRolePermissionBO.getPermissionIdList();
        Long roleId = authRolePermissionBO.getRoleId();
        permissionIdList.forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setIsDeleted(IsDeletedEnum.UN_DELETED_ENUM.getCode());
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermissionList.add(authRolePermission);
        });
        int count = authRolePermissionService.batchInsert(authRolePermissionList);
        return count > 0;
    }
}
